﻿using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;

namespace ScoutNET.Helpers
{
    public static class GenerateCaptchaHelper
    {
        public static MvcHtmlString GenerateCaptcha(this HtmlHelper helper, HttpContext context)
        {
            if (!context.Request.IsAuthenticated)
            {
                using (var captchaControl = new Recaptcha.RecaptchaControl
                {
                    ID = "recaptcha",
                    Theme = "clean",
                    PublicKey = ConfigurationManager.AppSettings["Recaptcha.PublicKey"],
                    PrivateKey = ConfigurationManager.AppSettings["Recaptcha.PrivateKey"]
                })
                {
                    using (var stringWriter = new StringWriter())
                    {
                        using (var htmlWriter = new HtmlTextWriter(stringWriter))
                        {
                            captchaControl.RenderControl(htmlWriter);
                            return MvcHtmlString.Create(htmlWriter.InnerWriter.ToString());
                        }
                    }
                }
            }
            else 
            {
                return MvcHtmlString.Empty;
            }
        }
    }
}
